package com.iceland.common.rpc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RpcServiceInterfaceFactory {

    private RpcMessageManager rpcMessageManager;
    public RpcServiceInterfaceFactory(RpcMessageManager rpcMessageManager) {
        this.rpcMessageManager=rpcMessageManager;
    }

    Map<String,RemoteMethodInvocationHandler> map=new ConcurrentHashMap<>();

    public  <T> T getBean(Class<T> _class){
        RemoteMethodInvocationHandler remoteMethodInvocationHandler=map.get(_class.getTypeName());
        if(remoteMethodInvocationHandler==null){
            try {
                remoteMethodInvocationHandler = new RemoteMethodInvocationHandler(_class,rpcMessageManager);
                map.put(_class.getTypeName(),remoteMethodInvocationHandler);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return remoteMethodInvocationHandler.getProxy();

    }


}
